package com.crape.account.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.crape.account.bo.IAccountBO;
import com.crape.common.model.AccountInfo;
import com.crape.common.model.Article;
import com.crape.common.model.Question;
import com.crape.common.utils.PageUtils;
import com.crape.faq.bo.IQuestionBO;
import com.crape.topic.bo.IArticleBO;

@Controller
public class SearchController {

	private IAccountBO accountBOImpl;
	private IQuestionBO questionBOImpl;
	private IArticleBO articleBOImpl;

	@Autowired
	public void setAccountBOImpl(IAccountBO accountBOImpl) {
		this.accountBOImpl = accountBOImpl;
	}

	@Autowired
	public void setQuestionBOImpl(IQuestionBO questionBOImpl) {
		this.questionBOImpl = questionBOImpl;
	}

	@Autowired
	public void setArticleBOImpl(IArticleBO articleBOImpl) {
		this.articleBOImpl = articleBOImpl;
	}

	@RequestMapping("/search/articles.jsp")
	public String articles(ModelMap modelMap, String searchValue, Integer pageNo) {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		modelMap.put("searchValue", searchValue);
		Long count = articleBOImpl.countSearchArticles(searchValue);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<Article> articles = articleBOImpl.searchArticles(searchValue, startIndex, pageSize);
			modelMap.put("articles", articles);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/search/articles.html";
	}

	@RequestMapping("/search/questions.jsp")
	public String questions(ModelMap modelMap, String searchValue, Integer pageNo) {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		modelMap.put("searchValue", searchValue);
		Long count = questionBOImpl.countSearchQuestions(searchValue);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<Question> questions = questionBOImpl.searchQuestions(searchValue, startIndex, pageSize);
			modelMap.put("questions", questions);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/search/questions.html";
	}

	@RequestMapping("/search/users.jsp")
	public String users(ModelMap modelMap, String searchValue, Integer pageNo) {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		modelMap.put("searchValue", searchValue);
		Long count = accountBOImpl.countSearchAccounts(searchValue);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<AccountInfo> accounts = accountBOImpl.searchAccounts(searchValue, startIndex, pageSize);
			modelMap.put("accounts", accounts);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/search/users.html";
	}

}
